package com.handpay.nocardpay.baselibrary.qualificationinfo.view.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.commbase.BaseProtocolKey;
import com.handpay.nocardpay.baselibrary.commbase.view.fragment.BaseFragment;
import com.handpay.nocardpay.baselibrary.main.mine.MainFragment;
import com.handpay.nocardpay.baselibrary.setting.view.fragment.GestureLockSettingFragment;
import com.handpay.nocardpay.baselibrary.utils.AccountUtils;
import com.handpay.nocardpay.baselibrary.utils.ClientEngine;

/**
 * Created by fplei on 2017/8/17.
 * 资质提交成功后展示界面
 */

public class QualificationSuccessFragment extends BaseFragment {
    private Button nextButton, qualification_setting_gesture_pwd_btn;
    private Toolbar mToolbar;
    public static int REQUEST_NEW_GESTURE_PWD = 2;      //新增手势密码

    public static QualificationSuccessFragment newInstance() {
        Bundle args = new Bundle();
        QualificationSuccessFragment fragment = new QualificationSuccessFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public String getFragmentTitle() {
        return getString(R.string.upload_success);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_qualification_success;
    }

    @Override
    public void initView(View view) {
        mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
        initToolbar(mToolbar, true, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goHome();
            }
        });

        nextButton = (Button) view.findViewById(R.id.nextButton);
        qualification_setting_gesture_pwd_btn = (Button) view.findViewById(R.id.qualification_setting_gesture_pwd_btn);
        if (!TextUtils.isEmpty(ClientEngine.getInstance().getRMSData(BaseProtocolKey.KEY_GESTURE_PWD))) {
            qualification_setting_gesture_pwd_btn.setVisibility(View.GONE);
        }
        TextView txt_qualification_tip1 = (TextView) view.findViewById(R.id.txt_qualification_tip1);
        space2(txt_qualification_tip1);
        TextView txt_qualification_tip2 = (TextView) view.findViewById(R.id.txt_qualification_tip2);
        space2(txt_qualification_tip2);
    }

    //首行缩进
    private void space2(TextView textView) {
        SpannableStringBuilder span = new SpannableStringBuilder("缩进" + textView.getText());
        span.setSpan(new ForegroundColorSpan(Color.TRANSPARENT), 0, 2,
                Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        textView.setText(span);
    }

    @Override
    public void initListener() {
        nextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goHome();
            }
        });

        qualification_setting_gesture_pwd_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goGesturePWDSetting();
            }
        });
    }

    private void goGesturePWDSetting() {
        AccountUtils.setAccountStatus(_mActivity, AccountUtils.AccountStatus.CHECKING);
        GestureLockSettingFragment fragment = GestureLockSettingFragment.newInstance(GestureLockSettingFragment.GESTURE_PWD_TYPE_FROM_QUALIFICATION);
        startWithPop(fragment);
    }

//    @Override
//    public void onFragmentResult(int requestCode, int resultCode, Bundle data) {
//        super.onFragmentResult(requestCode, resultCode, data);
//        if (requestCode == REQUEST_NEW_GESTURE_PWD) {
//            goHome();
//        }
//    }

    private void goHome() {
        AccountUtils.setAccountStatus(_mActivity, AccountUtils.AccountStatus.CHECKING);
        popTo(MainFragment.class, true);
        start(MainFragment.newInstance());

    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    @Override
    public boolean onBackPressedSupport() {
        goHome();
        return true;
    }
}
